package com.wzh.yebServe.controller;


import com.wzh.yebServe.pojo.LoginParams;
import com.wzh.yebServe.pojo.ResultBean;
import com.wzh.yebServe.service.IUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.security.Principal;

/**
 * @Author wzh
 * @create 2022/11/6 13:44
 * @Description:
 */
@RestController
@RequestMapping("/user")
@Api(tags = "LoginController  登录接口")
public class LoginController {

    @Autowired
    private IUserService iUserService;

    @ApiOperation("登录校验，登录后返回token")
    @PostMapping("/login")
    public ResultBean login(@RequestBody LoginParams loginParams, HttpServletRequest request){
        return  iUserService.login(loginParams.getUsername(),loginParams.getPassword(),loginParams.getCode(),request);
    }


     @ApiOperation("获取当前登录用户的信息")
    @PostMapping("/info")
     /**
      *principal是为object类型的，也就是说它可以接受所有的对象，
      */
    public ResultBean info(Principal principal){
        if (principal==null){
            return null;
        }
        String username = principal.getName();

        return  iUserService.getInfo(username);
    }


    @ApiOperation(value = "退出登录")
    @GetMapping("/logout")
    public ResultBean logout(){
        return ResultBean.success("退出成功");
    }

}
